package com.zkadmin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zk.ui.event.OpenEvent;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;
import org.zkoss.zul.Tab;
import org.zkoss.zul.Tabs;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Window;
import org.zkoss.zul.impl.LabelElement;

import com.zkadmin.renderer.SysMenuTreeitemRenderer;
import com.zkadmin.service.SysMenuService;
import com.zkadmin.service.SysUserService;
import com.zkadmin.util.AdminConstants;
import com.zkadmin.util.AdminWebUtils;

@SuppressWarnings("serial")
public class IndexCtrl extends BaseCtrl<Window> {
	Logger logger = LoggerFactory.getLogger(IndexCtrl.class);

	@WireVariable
	private SysMenuService sysMenuService;

	@WireVariable
	private SysUserService sysUserService;

	@Wire
	private Tabs tabs;

	@Wire
	private Tree menuTree;

	@Wire
	private Menupopup tabMenuPopup;

	private Tab rightClickTab;

	@Wire
	private Label lbLoginUserName;

	private Window win;

	@Override
	protected void init(Window win) {
		this.win = win;
		menuTree.setItemRenderer(new SysMenuTreeitemRenderer());
		menuTree.setModel(sysMenuService.getMenuTreeModel(true));
		AdminWebUtils.openPage("home.zul", AdminConstants.getHomeLabel(), "z-icon-home");
		lbLoginUserName.setValue(sysUserService.getCurrentSysUser().getName());
	}

	@Listen("onOpen = #tabMenuPopup")
	public void tabsMenuPopup(OpenEvent event) {
		rightClickTab = (Tab) event.getReference();
		if (rightClickTab != null) {
			Menuitem menuitem = (Menuitem) tabMenuPopup.getFellow("menuItemCloseCurrentTab");
			menuitem.setDisabled(rightClickTab.getLabel().equals(AdminConstants.getHomeLabel()));
		}
	}

	@Listen("onClick = #tabMenuPopup > menuitem")
	public void menuAction(MouseEvent event) {
		final String menuitemId = ((LabelElement) event.getTarget()).getId();
		List<Tab> tmpTabList = tabs.getChildren();
		List<Tab> tabList = new ArrayList<Tab>(tmpTabList);
		for (Tab tab : tabList) {
			if (!tab.getLabel().equals(AdminConstants.getHomeLabel())) {
				if (menuitemId.equals("menuItemCloseCurrentTab") && rightClickTab.getLabel().equals(tab.getLabel())) {
					tab.close();
				} else if (menuitemId.equals("menuItemCloseOtherTab") && !rightClickTab.getLabel().equals(tab.getLabel())) {
					tab.close();
				} else if (menuitemId.equals("menuItemCloseAllTab")) {
					tab.close();
				}
			}

			if (menuitemId.equals("menuItemReloadTab") && rightClickTab.getLabel().equals(tab.getLabel())) {
				tab.setSelected(true);
				((Include) tab.getLinkedPanel().getFirstChild()).invalidate();
			}
		}
	}

	@Listen("onClick=#refreshTreeBtn")
	public void refreshTree() {
		menuTree.setModel(sysMenuService.getMenuTreeModel(true));
	}

	@Listen("onClick=#lbResetPwd")
	public void resetPwd() {
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("userId", sysUserService.getCurrentSysUser().getId());
		Window window = (Window) Executions.createComponents(AdminWebUtils.getZulPagePath("admin/reset_user_pwd_dialog.zul"), win, args);
		window.doModal();
	}

	@Listen("onClick=#lbLogout")
	public void logout() {
		SecurityUtils.getSubject().logout();
		Executions.sendRedirect("/login.zul");
	}
}
